package sg.edu.nus.iss.team8s.vmcs.commons;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import java.util.Properties;
import java.io.FileOutputStream;
import java.io.FileInputStream;
/**
 * @author Zheng Xiaofeng
 */
public class CommonUtility {
	
	public static String parseFromNumber(double number, String format){
		DecimalFormat numberFormat = new DecimalFormat(format);
		return numberFormat.format(number);
	}
	
	public static double parseToNumber(String number, String format) throws Exception {
		DecimalFormat numberFormat = new DecimalFormat(format);
		return numberFormat.parse(number).doubleValue();
	}
	
	public static String parseFromDate(Date date, String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}
	
	public static Date parseToDate(String date, String format) throws Exception{
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.parse(date);
	}
	
	public static ArrayList<String> parseTokens(String line, String delim) {		
		ArrayList<String> tokenList = new ArrayList<String>();
		StringTokenizer tokenizer = new StringTokenizer(line, delim);
		while (tokenizer.hasMoreTokens()) 
		{
	         tokenList.add(tokenizer.nextToken().trim());
	    }
		return tokenList;
	}
	
	public static boolean within(Date startDate, int days, Date date) throws Exception{
		if(days == -1 && !date.before(startDate)){
			return true;
		}
		else{
			GregorianCalendar endDate = new GregorianCalendar();
			endDate.setTime(startDate);
			endDate.add(Calendar.DAY_OF_MONTH, days);
			if(!date.before(startDate) && !date.after(endDate.getTime())){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
	public static void alignScreenCenter(Component comp){
		Toolkit toolkit = Toolkit.getDefaultToolkit();
	    Dimension ssize = toolkit.getScreenSize();
	    int x = (int) ( ssize.getWidth() - comp.getWidth() ) / 2;
	    int y = (int) ( ssize.getHeight() - comp.getHeight() ) / 2;
	    comp.setLocation(x, y);
	}
	
	public static void disposeWhenWindowClosing(final Frame frame){
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				frame.setVisible(false);
				frame.dispose();
			}
		});
	}
	
	public static void storeSystemProperties(String xmlFile, Properties props) throws Exception{
		FileOutputStream os = new FileOutputStream(xmlFile);
		props.storeToXML(os, "XML File for system properties of store application");
	}
	
	public static Properties loadSystemProperties(String xmlFile) throws Exception{
		FileInputStream in = new FileInputStream(xmlFile);
		Properties props = new Properties();
		props.loadFromXML(in);
		return props;
	}
}
